﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class CopyMeetBossBox : MonoBehaviour {

    public UILabel m_kLabel;
    public List<UITweener> tweeners;
    public UITexture heroTexture;

    string textureResUrl = "";

    void OnEnable()
    {

    }

    public void Init(string heroId,int level)
    {
        gameObject.SetActive(true);
        Dict.DataModel.TableHero heroData = Dict.Blo.DictHeroBlo.GetTableHero(heroId);
        textureResUrl = "hero2d/" + heroData.iconName.Replace("icon","2d");
        StartCoroutine(LoadTexture());

        m_kLabel.text = string.Format(TableTextBlo.GetText("meet_boss_tip"),
            "[c40000]" + TableTextBlo.GetText(heroData.name) + "【等级." + level.ToString() + "】[-]");
        foreach (var tweener in tweeners)
        {
            tweener.ResetToBeginning();
            tweener.PlayForward();
        }
    }

    IEnumerator LoadTexture()
    {
        AssetLoad.AssetBundleManager.Instance.LoadAssetBundle(textureResUrl);
        while (!AssetLoad.AssetBundleManager.Instance.IsLoadedAssetBundle(textureResUrl))
        {
            yield return 1;
        }
        if (heroTexture != null)
        {
            heroTexture.mainTexture = AssetLoad.AssetBundleManager.Instance.GetAssetFromBundle(textureResUrl, CommonMethod.GetAssetNameFromAssetBundleName(textureResUrl), typeof(Texture2D)) as Texture2D;
        }
    }

    void UnloadTexture()
    {
        heroTexture.mainTexture = null;
        AssetLoad.AssetBundleManager.Instance.UnloadAssetBundle(textureResUrl);
        textureResUrl = "";
    }

    public void OnConfirmClick()
    {
        UI.PageManager.instance.OpenPage("MercenaryTaskPage","tab=1");
        Destroy(gameObject);
    }
    public void OnCancelClick()
    {
        Destroy(gameObject);
    }

    void Destroy()
    {
        StopAllCoroutines();
        UnloadTexture();
    }
}
